<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\admin\model\card\Code as CardCodeModel;
use app\admin\model\Order as OrderModel;
use app\admin\model\reward\Log as RewardLogModel;
use app\admin\model\Withdrawlapply as WithdrawlapplyModel;
use app\common\model\User as UserModel;

/**
 * 数据统计
 *
 * @icon fa fa-circle-o
 */
class Statistics extends Backend
{

    /**
     * Statistics模型对象
     * @var \app\admin\model\Statistics
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('User');

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    
     /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->where($where)
                ->where('is_agent',1)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $k => $v) {
                
                $v->user_card_count = CardCodeModel::where('agent_id',$v['id'])->count();
                $v->user_active_card_count = CardCodeModel::where('agent_id',$v['id'])->where('is_exchange','1')->count();
                $v->travel_count = OrderModel::where('zt_user_id',$v['id'])->where('status','1')->count();
                $v->total_profit = RewardLogModel::where('agent_id',$v['id'])->where('status','2')->sum('money');
                $v->agent_profit = RewardLogModel::where('agent_id',$v['id'])->where('status','2')->where('type','2')->sum('money');
                $v->travel_profit = RewardLogModel::where('agent_id',$v['id'])->where('status','2')->where('type','1')->sum('money');
                $v->user_money = $v['money'];
                $v->with_money = WithdrawlapplyModel::where('user_id',$v['id'])->where('state','1')->sum('money');
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        $return['user_card_count'] = CardCodeModel::count();
        $return['user_active_card_count'] = CardCodeModel::where('is_exchange','1')->count();
        $return['travel_count'] = OrderModel::where('status','1')->count();
        $return['total_profit'] = RewardLogModel::where('status','2')->sum('money');
        $return['agent_profit'] = RewardLogModel::where('status','2')->where('type','2')->sum('money');
        $return['travel_profit'] = RewardLogModel::where('status','2')->where('type','1')->sum('money');
        $return['user_money'] = UserModel::sum('money');
        $return['with_money'] = WithdrawlapplyModel::where('state','1')->sum('money');
         $this->view->assign("data", $return);
        return $this->view->fetch();
    }


}
